<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { getComboDataApi } from "@/api/combo-web"
import { type IGetComboData } from "@/api/combo/types/combo"
import { type FormInstance, ElMessage } from "element-plus"
import { Search, Refresh, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useUserStore } from "@/store/modules/user"
import useClipboard from "vue-clipboard3"
import { useCommonStore } from "@/store/modules/common"
import vueQr from "vue-qr/src/packages/vue-qr.vue"
defineOptions({
  name: "ComboList"
})
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const userStore = useUserStore()
const commonStore = useCommonStore()
//#region 增
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = reactive<any>({ data: {} })
//#endregion
//#region 查
const tableData = ref<IGetComboData[]>([])
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  name: "",
  sku: "",
  superSku: "",
  state: "true"
  // team: userStore.teamName ? userStore.teamName : ""
})
const getTableData = () => {
  loading.value = true
  getComboDataApi({
    page: paginationData.currentPage,
    size: paginationData.pageSize,
    name: searchData.name,
    // teamId: userStore.usernameID
    teamId: userStore.userInfo.user_id
  })
    .then((res) => {
      paginationData.total = res.data.total
      tableData.value = res.data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const handleRefresh = () => {
  getTableData()
}

//查看详情
const handleSeeInfo = (row: any) => {
  formData.data = row
  dialogVisible.value = true
}
const dialogVisible1 = ref<boolean>(false)
//查看大图
const seeZool = () => {
  dialogVisible1.value = true
}

//分享

/**分享产品 */
const dialogVisibleShare = ref<boolean>(false)
const link = ref<string>("")
const handleShare = (row: any) => {
  dialogVisibleShare.value = true
  link.value = `${commonStore.BASEURL}/retail/#/productWebDetil?p=${row.id}&u=${userStore.userInfo.user_id}`
  // link.value = `http://192.168.1.7:3336/#/productDetil?p=${row.id}&u=${userStore.usernameID}`
}
const { toClipboard } = useClipboard()
const copy = async (text: string) => {
  try {
    await toClipboard(text) //实现复制
    ElMessage.success("已复制产品链接！")
  } catch (e) {
    console.error(e)
  }
}
/**END */
/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
import { getWebSteDataApi } from "@/api/webSet"
const comboList = ref<any>([])
getWebSteDataApi({
  page: 1,
  size: 10000
})
  .then((res: any) => {
    comboList.value = res.data.list
  })
  .catch(() => {
    comboList.value = []
  })
/**department END */
// console.log(departTeam.value)

const formatterCombo = (row: any, column: any, celValue: any) => {
  comboList.value.filter((item: any) => {
    if (item.id == row.systemId) {
      celValue = item.name
    }
  })
  return celValue
}
</script>

<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="套餐名">
          <el-input v-model.trim="searchData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新表格">
            <el-button type="primary" :icon="RefreshRight" circle @click="handleRefresh" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="id" label="ID" align="center" />
          <!-- <el-table-column prop="teamName" label="团队名" align="center" /> -->
          <el-table-column prop="photo" label="是否上传身份证" align="center" width="280">
            <template #default="scope">
              <span v-if="scope.row.photo"> 是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="套餐名" align="center" />
          <el-table-column prop="systemId" label="系统对应套餐" align="center" :formatter="formatterCombo" width="220" />
          <el-table-column fixed="right" label="操作" width="280" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleSeeInfo(scope.row)">查看详情</el-button>
              <el-button type="success" text bg size="small" @click="handleShare(scope.row)">分享产品</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination background :layout="paginationData.layout" :page-sizes="paginationData.pageSizes"
          :total="paginationData.total" :page-size="paginationData.pageSize" :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </el-card>
    <!--查看详情 -->
    <el-dialog v-model="dialogVisible" title="查看详情" width="80%">
      <el-form ref="formRef" :model="formData.data" label-width="100px" label-position="left">
        <el-form-item prop="name" label="套餐名">
          <el-input v-model="formData.data.name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item prop="systemId" label="系统对应套餐">
          <el-select v-model="formData.data.systemId" disabled style="width: 100%">
            <el-option v-for="item in comboList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item prop="image" label="H5背景图">
          <el-image @click="seeZool" style="width: 200px; height: 200px; cursor: pointer"
            :src="`${commonStore.BASEURL}/image/${formData.data.image}`" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
    <el-dialog v-model="dialogVisible1" @close="dialogVisible1 = false">
      <img w-full :src="`${commonStore.BASEURL}/image/${formData.data.image}`" alt="Preview Image" />
    </el-dialog>

    <el-dialog v-model="dialogVisibleShare" title="分享产品" width="50%">
      <div>
        <span>产品链接：</span>
        <el-link>{{ link }}</el-link>
        <el-link type="primary" style="margin-left: 10px" @click="copy(link)"><el-icon>
            <DocumentCopy />
          </el-icon>复制</el-link>
      </div>
      <div style="margin-top: 30px">
        <span style="vertical-align: top">产品二维码：</span>
        <vue-qr :text="link" :size="200" />
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;

  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.ageInput :deep(.el-input) {
  width: 20% !important;
}

:deep(.el-form-item__label) {
  width: 130px !important;
}

:deep(.el-popper) {
  max-width: 50% !important;
}

:deep(.el-cascader) {
  width: 567px !important;
}

.text-ellipsis {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  display: -webkit-box;
  -webkit-box-orient: vertical;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}

.el-popper.is-customized {
  /* Set padding to ensure the height is 32px */
  padding: 6px 12px;
  background: linear-gradient(90deg, rgb(159, 229, 151), rgb(204, 229, 129));
}

.el-popper.is-customized .el-popper__arrow::before {
  background: linear-gradient(45deg, #b2e68d, #bce689);
  right: 0;
}
</style>
